package net.shopxx.action.shop;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.sf.json.JSONArray;
import net.shopxx.bean.Pager;
import net.shopxx.common.CommentUtil;
import net.shopxx.entity.Area;
import net.shopxx.entity.Article;
import net.shopxx.entity.ArticleCategory;
import net.shopxx.entity.Brand;
import net.shopxx.entity.Groupbuy;
import net.shopxx.entity.Product;
import net.shopxx.entity.ProductCategory;
import net.shopxx.entity.ProductComment;
import net.shopxx.service.AreaService;
import net.shopxx.service.ArticleCategoryService;
import net.shopxx.service.ArticleService;
import net.shopxx.service.BrandService;
import net.shopxx.service.GroupbuyService;
import net.shopxx.service.ProductCategoryService;
import net.shopxx.service.ProductService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.util.StringUtil;
import com.opensymphony.oscache.web.ServletCacheAdministrator;

/**
 * 后台Action类 - 商品评论
 */

@ParentPackage("shop")
public class GroupbuyAction extends BaseShopAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8982038175671094442L;

	private static final String acId= "402881e53a58bea4013a5905de07000c";
	@Resource
	private ArticleService articleService;
	@Resource
	private ArticleCategoryService articleCategoryService;
	@Resource
	private GroupbuyService groupbuyService;
	@Resource
	private ProductCategoryService productCategoryService;
	@Resource
	private BrandService brandService;
	@Resource
	private ProductService productService;
	@Resource
	private AreaService areaService;
	
	private String sproductCategory;
	private String sbrand;
	private String skeyword;
	private String areaId;
	private Area area;
	private List<Area> areaList;
	private List<Article> articleList;
	private Groupbuy groupbuy;
	// 列表
	@SuppressWarnings("unchecked")
	public String searchProduct() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Product.class);
		if(!StringUtil.isEmpty(sproductCategory)) 
		detachedCriteria.add(Restrictions.eq("productCategory.id", sproductCategory));
		if(!StringUtil.isEmpty(sbrand))
		detachedCriteria.add(Restrictions.eq("brand.id", sbrand));
		if(pager == null)pager = new Pager();
		if(!StringUtil.isEmpty(skeyword)){
			pager.setProperty("name");
			pager.setKeyword(skeyword);
		}
		
		pager.setPageSize(Integer.MAX_VALUE);
		pager = productService.findByPager(pager,detachedCriteria);
		List<Product> ls = pager.getList();
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		for (Product product : ls) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap.put("id", product.getId());
			jsonMap.put("name", product.getName());
			mapList.add(jsonMap);
		}
		
		
		JSONArray jsonArray = JSONArray.fromObject(mapList);
		return ajax(jsonArray.toString(), "text/html"); 
	}
	
	private void initGroupbuyData()
	{
		areaList = areaService.getRootAreaList();

		if(!StringUtil.isEmpty(areaId))
			area = areaService.get(areaId);
		
		if(area == null && !areaList.isEmpty())
		{
			area = areaList.get(0);
		}
	}

	// 列表
	public String list() {
		initGroupbuyData();
		if(pager==null)
		{
			pager = new Pager();
		}
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Groupbuy.class);
		detachedCriteria.add(Restrictions.like("areaPath", "%"+area.getPath() + "%"));
		if(groupbuy != null && groupbuy.getCategory() != null)
		{
			detachedCriteria.add(Restrictions.eq("category", groupbuy.getCategory()));
		}
		if(!StringUtil.isEmpty(skeyword))
		{
			if(skeyword.contains("~"))
			{
				String[] prices = skeyword.split("~");
				if(prices.length==1)
				{
					detachedCriteria.add(Restrictions.ge("price", Double.parseDouble(prices[0])));
				}
				else
				{
					if(!StringUtil.isEmpty(prices[0]))
					{
						detachedCriteria.add(Restrictions.ge("price", Double.parseDouble(prices[0])));
					}
					detachedCriteria.add(Restrictions.le("price", Double.parseDouble(prices[1])));
				}
			}
		}
		pager = groupbuyService.findByPager(pager,detachedCriteria);
		return LIST;
	}
	
	// 列表
	public String info() {
		ArticleCategory articleCategory = articleCategoryService.load(acId);
		articleList = articleService.getArticleList(articleCategory,0,10);
		initGroupbuyData();
		if(pager==null)
		{
			pager = new Pager();
		}
		groupbuy = groupbuyService.load(id);
		
		getRequest().setAttribute("productComment", calculateComment(groupbuy.getProduct().getProductCommentSet()));
		return VIEW;
	}
	

	
	public CommentUtil calculateComment(Set<ProductComment> productCommentSet)
	{
		int goodNum=0,midNum=0,badNum=0,total=0;
		for (ProductComment productComment : productCommentSet) {
			total++;
			switch(productComment.getRank())
			{
				case 0:
				case 1:
				case 2:
					badNum++;
					break;	
				case 3:
				case 4:
					midNum++;
					break;	
				case 5:
					goodNum++;
					break;	
				default:
					midNum++;
					break;
			}
		}
		return new CommentUtil(badNum,midNum,goodNum,total);
	}
	
	// 获取商品分类树
	public List<ProductCategory> getProductCategoryTreeList() {
		return productCategoryService.getProductCategoryTreeList();
	}
	// 获取所有品牌
	public List<Brand> getAllBrand() {
		return brandService.getAll();
	}
	
	
	public Groupbuy getGroupbuy() {
		return groupbuy;
	}
	public void setGroupbuy(Groupbuy groupbuy) {
		this.groupbuy = groupbuy;
	}
	
	
	public String getSproductCategory() {
		return sproductCategory;
	}

	public void setSproductCategory(String sproductCategory) {
		this.sproductCategory = sproductCategory;
	}

	public String getSbrand() {
		return sbrand;
	}

	public void setSbrand(String sbrand) {
		this.sbrand = sbrand;
	}

	public String getSkeyword() {
		return skeyword;
	}

	public void setSkeyword(String skeyword) {
		this.skeyword = skeyword;
	}

	public List<Area> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<Area> areaList) {
		this.areaList = areaList;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public String getAreaId() {
		return areaId;
	}

	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}

	public List<Article> getArticleList() {
		return articleList;
	}

	public void setArticleList(List<Article> articleList) {
		this.articleList = articleList;
	}

	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	

}